﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar;
using RuoVea.ExSugar.Entity;
using RuoVea.OmiApi.Dict.Entitys;
using RuoVea.OmiApi.Dict.Service;
using RuoVea.OmiApi.Dict.Service.Dto;
using RuoVea.OmiDict.Attributes;
using RuoVea.OmiDict.Language;
using System;

namespace RuoVea.OmiDict.Controllers;
/// <summary>
/// 
/// </summary>
public class SysDictValuePageParam : SysDictValueParam
{
    private int _currentPage;
    /// <summary>
    /// 
    /// </summary>
    public int CurrentPage
    {
        get { return _currentPage; }
        set
        {
            _currentPage = value;
            base.PageNo = value;
        }
    }
}
/// <summary>
/// 字典值管理
/// </summary>
[AppRoute]
public class DictValueController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SysDictTypeService _sysDictTypeService;
    private readonly SugarRepository<SysDictType> _thisDictTypeRepository;
    private readonly SysDictValueService _sysDictValueService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysDictValueService"></param>
    /// <param name="thisDictTypeRepository"></param>
    /// <param name="sysDictTypeService"></param>
    public DictValueController(SysDictValueService sysDictValueService, SugarRepository<SysDictType> thisDictTypeRepository, SysDictTypeService sysDictTypeService)
    {
        _sysDictValueService = sysDictValueService;
        _thisDictTypeRepository = thisDictTypeRepository;
        _sysDictTypeService = sysDictTypeService;
    }

    /// <summary>
    /// 参数配置页面-动态组件
    /// </summary>
    [HttpGet("index/{typeId:long}")]
    public IActionResult Index(long typeId, string typeName)
    {
        ViewBag.typeId = typeId;
        ViewBag.typeName = typeName;
        return View();
    }

    /// <summary>
    /// 参数配置页面-常规组件
    /// </summary>
    [HttpGet("nomal/{typeId:long}")]
    public IActionResult Nomal(long typeId, string typeName)
    {
        ViewBag.typeId = typeId;
        ViewBag.typeName = typeName;
        return View();
    }

    /// <summary>
    /// 参数配置页面
    /// </summary>
    /// <param name="typeCode"></param>
    /// <param name="t"></param>
    /// <param name="aed"></param> 
    /// <param name="uploadUrl"></param>
    /// <param name="previewUrl"></param>
    /// <returns></returns>
    [HttpGet("custom/{typeCode}")]
    [HttpGet("customt/{typeCode}/{t}")]
    [HttpGet("customtaed/{typeCode}/{t}/{aed}")]
    public async Task<IActionResult> Custom(string typeCode, string t, string aed, string uploadUrl, string previewUrl)
    {
        try
        {
            var dictType = await _sysDictTypeService.GetTypeByCodeAsync(typeCode);
            ViewBag.typeId = dictType?.Id;
            ViewBag.typeName = dictType?.Name;
            ViewBag.tagType = t;

            if (string.IsNullOrWhiteSpace(uploadUrl) || string.IsNullOrWhiteSpace(previewUrl))
            {
                var dictValues = await _sysDictValueService.GetDictDataListByCodeCache("FILE_UPLOAD");
                ViewBag.previewUrl = dictValues.Where(x => x.Code == "FILE_PREVIEW_URL").Select(x => x.Value).FirstOrDefault();
                ViewBag.uploadUrl = dictValues.Where(x => x.Code == "FILE_UPLOAD_URL").Select(x => x.Value).FirstOrDefault();
            }
            else
            {
                ViewBag.previewUrl = previewUrl;
                ViewBag.uploadUrl = uploadUrl;
            }
            if (string.IsNullOrWhiteSpace(aed))
            {
                ViewBag.addBtn = "";
                ViewBag.editBtn = "";
                ViewBag.delBtn = "";
            }
            else
            {
                ViewBag.addBtn = aed.Contains("a") ? "a" : "";
                ViewBag.editBtn = aed.Contains("e") ? "e" : "";
                ViewBag.delBtn = aed.Contains("d") ? "d" : "";
            }
            return View();
        }
        catch
        {
            return BadRequest(string.Format(i18n.unsetvalueswitch, typeCode));
        }
    }

    /// <summary>
    /// 参数配置页面
    /// FILE_UPLOAD->FILE_PREVIEW_URL,FILE_UPLOAD_URL
    /// </summary>
    [HttpGet("image/{typeCode}")]
    [HttpGet("imaged/{typeCode}/{aed}")]
    public async Task<IActionResult> Image(string typeCode, string aed, string uploadUrl, string previewUrl)
    {
        try
        {
            var dictType = await _sysDictTypeService.GetTypeByCodeAsync(typeCode);
            ViewBag.typeId = dictType?.Id;
            ViewBag.typeName = dictType?.Name;

            if (string.IsNullOrWhiteSpace(uploadUrl) || string.IsNullOrWhiteSpace(previewUrl))
            {
                var dictValues = await _sysDictValueService.GetDictDataListByCodeCache("FILE_UPLOAD");
                ViewBag.previewUrl = dictValues.Where(x => x.Code == "FILE_PREVIEW_URL").Select(x => x.Value).FirstOrDefault();
                ViewBag.uploadUrl = dictValues.Where(x => x.Code == "FILE_UPLOAD_URL").Select(x => x.Value).FirstOrDefault();
            }
            else
            {
                ViewBag.previewUrl = previewUrl;
                ViewBag.uploadUrl = uploadUrl;
            }
            if (string.IsNullOrWhiteSpace(aed))
            {
                ViewBag.addBtn = "";
                ViewBag.editBtn = "";
                ViewBag.delBtn = "";
            }
            else
            {
                ViewBag.addBtn = aed.Contains("a") ? "a" : "";
                ViewBag.editBtn = aed.Contains("e") ? "e" : "";
                ViewBag.delBtn = aed.Contains("d") ? "d" : "";
            }
            return View();
        }
        catch
        {
            return BadRequest(string.Format(i18n.unsetvalueswitch, typeCode));
        }
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysDictValueDto>>> GetPagesAsync(SysDictValuePageParam data)
    {
        RestfulResult<PageResult<SysDictValueDto>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _sysDictValueService.GetPagesAsync(data);
        return restfulResult;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(SysDictValueDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            var typeData=await _thisDictTypeRepository.GetFirstAsync(x => x.Id == data.TypeId);
            data.TypeCode = typeData.Code;
            await _sysDictValueService.AddDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(SysDictValueDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            var typeData = await _thisDictTypeRepository.GetFirstAsync(x => x.Id == data.TypeId);
            data.TypeCode = typeData.Code;
            await _sysDictValueService.UpdateDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 验证字典值数据的必填规则
    /// </summary>
    /// <param name="tagType"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentException"></exception>
    [HttpGet("validatevalue")]
    public virtual RestfulResult GetValidateValue(string tagType, string value)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            _sysDictValueService.GetValidateValue(tagType, value);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 验证字典值数据的必填规则
    /// </summary>
    /// <param name="tagType"></param>
    /// <param name="extData"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentException"></exception>
    [HttpGet("validateextdata")]
    public virtual RestfulResult GetValidateExtData(string tagType, string extData)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            _sysDictValueService.GetValidateExtData(tagType, extData);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteDataAsync(EntityBaseId data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _sysDictValueService.DeleteDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Message = ex.Message;
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            return restfulResult;
        }
    }
}